package com.zam.web.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @Author talent_dog
 * @Date 2024/1/25 15:13
 * @Version 1.0
 */
@RestController
@RequestMapping("/capture")
public class CaptureController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //验证码
    @GetMapping("/chars")
    public void chars(@RequestParam("key") String key, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //字符验证码
//        SpecCaptcha captcha = new SpecCaptcha(130,35,2);
//        captcha.setCharType(SpecCaptcha.TYPE_NUM_AND_UPPER);
//        //前后端分离系统，session不能使用
//        //存入redis中，便于验证
//        stringRedisTemplate.opsForValue().set(key,captcha.text().toLowerCase(),60, TimeUnit.SECONDS);
//        CaptchaUtil.out(captcha,request,response);

        //算数验证码
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 35, 3);
        captcha.setLen(2);
//        captcha.getArithmeticString(); //获取运算公式
        String value = captcha.text();  //获取结果
        stringRedisTemplate.opsForValue().set(key, value, 60, TimeUnit.SECONDS);
        CaptchaUtil.out(captcha, request, response);
    }
}
